package model.micro
{
	import flash.utils.SetIntervalTimer;
	
	/**
	 * Used for collision checking.
	 */
	public class BoundingBox
	{
		/**X coordinate of bottom left corner*/
		public var left:Number;
		/**y coordinate of bottom left corner*/
		public var bottom:Number;
		/**Width*/
		public var width:Number;
		/**Height*/
		public var height:Number;
		/**Optional place to put a reference back to the owning object.*/
		public var owner:Object;
		
		public function BoundingBox(x:Number, y:Number, width:Number, height:Number)
		{
			this.left = x;
			this.bottom = y;
			this.width = width;
			this.height = height;
		}
		
		/**
		 * The x coordinate of the right edge of the box.
		 */
		public function get right():Number
		{
			return left + width;
		}
		
		/**
		 * set the x coordinate of the right edge of the box.
		 */
		public function set right(n:Number):void
		{
			left = n - width;
		}
		
		/**
		 * The y coordinate of the bottom edge of the box.
		 */
		public function get top():Number
		{
			return bottom + height;
		}
		
		/**
		 * set the y coordinate of the top edge of the box.
		 */
		public function set top(n:Number):void
		{
			bottom = n - height;
		}
		
		/**
		 * The x coordinate of the center of the box.
		 */
		public function get center_x():Number
		{
			return left + width / 2;
		}
		
		/**
		 * The y coordinate of the center of the box.
		 */
		public function get center_y():Number
		{
			return bottom + height / 2;
		}
		
		/**
		 * Move the bounding box.
		 * @param	dx	x distance
		 * @param	dy	y distance
		 */
		public function move(dx:Number, dy:Number):void
		{
			left += dx;
			right += dy;
		}
		
		/**
		 * @return	true if the point exists in the box.
		 */
		public function testPoint(test_x:Number, test_y:Number):Boolean
		{
			return (left < test_x && test_x < right && bottom < test_y && test_y < top);
		}
		
		/**
		 * Checks if this box collides with another.
		 * @param	other	the box to check with
		 * @return	true if they collide, else false.
		 */
		public function testCollision(other:BoundingBox):Boolean
		{
			return (this.left < other.right && this.right > other.left && this.top > other.bottom && this.bottom < other.top);
		}
		
		/**
		 * @return	left, bottom, width, height
		 */
		public function toString():String
		{
			return "BoundingBox(" + left + "," + bottom + "," + width + "," + height + ")";
		}
	
	}

}